package practice;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-11-11
 * Time: 19:24
 */

//冒泡排序
public class bubbleSort {
    public static void main(String[] args) {
        int arr[] = {5,4,3,2,1};
        //长度是6 下标是5
        int tmp = 0;
        boolean flag = false;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length-i-1; j++) {
                if (arr[j] > arr[j+1]){
                    flag = true;
                    //交换
                    tmp = arr[j+1];
                    arr[j+1] = arr[j];
                    arr[j] = tmp;
                }
            }
            if (!flag){
                //如果一次都没交换 证明已经有序了
                break;
            }else {
                //否则就重置一下
                flag = false;
            }
        }
        System.out.println(Arrays.toString(arr));
    }

    //总结来看就是双层循环 第二层 都是从头开始比较 走一层 少一个数
}
